package com.example.gw_back_end.utils;

import java.util.Objects;

public class ThreadLocalUtil {

    // 提供ThreadLocal对象
    private static final ThreadLocal<Object> THREAD_LOCAL = new ThreadLocal<>();

    // 根据键获取值
    @SuppressWarnings("unchecked")
    public static <T> T get() {
        Object value = THREAD_LOCAL.get();
        return (T) value;
    }

    // 存储键值对
    public static void set(Object value) {
        THREAD_LOCAL.set(value);
    }

    // 清除ThreadLocal 防止内存泄露
    public static void remove() {
        THREAD_LOCAL.remove();
    }

    // 检查当前线程对应的ThreadLocal中是否存在值
    public static boolean hasValue() {
        return Objects.nonNull(THREAD_LOCAL.get());
    }
}
